/**
 * Created by sean on 15-7-20.
 */
dojo.require("dojox.gfx");
function SkyPlot(divId, refreshInterval, callback, overallDiv, warningDiv) {
    this.divId = divId;
    this.overallDiv = overallDiv;
    this.warningDiv = warningDiv;
    this.container = dojo.byId(divId);
    SkyPlot.xmlCallback = callback;
    SkyPlot.refreshInterval = refreshInterval;
    if (!SkyPlot.plots) SkyPlot.plots = [];
    SkyPlot.plots.push(this);
}
SkyPlot.prototype.restart = function(width, height) {
    var nominalWidth = 500;
    var nominalHeight = 400;
    var scale;
    if (width / height > nominalWidth / nominalHeight) scale = height / 400;
    else scale = width / 500;
    this.scale = scale;
    if (this.surface) this.surface.destroy();
    this.svElem = new Array();
    this.legendElem = new Array();
    var surface = this.surface = dojox.gfx.createSurface(this.container, nominalWidth * scale, nominalHeight * scale);
    this.gridColor = jQuery('html').css('color');
    var cx = 220 * scale;
    var cy = 200 * scale;
    var radius = 180 * scale;
    var stroke = Math.floor(1 + scale * 2);
    surface.createCircle({
        cx: cx,
        cy: cy,
        r: radius
    }).setStroke({
        color: this.gridColor,
        width: stroke
    });
    surface.createCircle({
        cx: cx,
        cy: cy,
        r: radius * 2 / 3
    }).setStroke({
        color: this.gridColor,
        width: stroke
    });
    surface.createCircle({
        cx: cx,
        cy: cy,
        r: radius / 3
    }).setStroke({
        color: this.gridColor,
        width: stroke
    });
    for (i = 0; i < 6; ++i) {
        var angle = (i * 30) * Math.PI / 180;
        x1 = radius * Math.cos(angle) + cx;
        y1 = radius * Math.sin(angle) + cy;
        x2 = radius * Math.cos(angle + Math.PI) + cx;
        y2 = radius * Math.sin(angle + Math.PI) + cy;
        surface.createLine({
            x1: x1,
            y1: y1,
            x2: x2,
            y2: y2
        }).setStroke({
            color: this.gridColor,
            width: stroke
        });
    }
    this.addLabel(scale * 215, scale * 15, "0");
    this.addLabel(scale * 310, scale * 44, "30");
    this.addLabel(scale * 376, scale * 110, "60");
    this.addLabel(scale * 403, scale * 205, "90");
    this.addLabel(scale * 380, scale * 295, "120");
    this.addLabel(scale * 310, scale * 370, "150");
    this.addLabel(scale * 207, scale * 395, "180");
    this.addLabel(scale * 105, scale * 370, "210");
    this.addLabel(scale * 35, scale * 295, "240");
    this.addLabel(scale * 10, scale * 205, "270");
    this.addLabel(scale * 35, scale * 110, "300");
    this.addLabel(scale * 105, scale * 44, "330");
    this.addLabel(scale * 222, scale * 76, "30");
    this.addLabel(scale * 222, scale * 136, "60");
    SkyPlot.getXmlData();
};
SkyPlot.prototype.addLabel = function(x, y, label) {
    this.surface.createText({
        x: x,
        y: y,
        text: label
    }).setFill(this.gridColor).setFont({
        family: "Arial",
        size: Math.floor(15 * this.scale) + "px"
    });
};
SkyPlot.pageTimeout;
SkyPlot.loading = false;
SkyPlot.getXmlData = function() {
    clearTimeout(SkyPlot.pageTimeout);
    if (!SkyPlot.loading) {
        SkyPlot.loading = true;
        getXmlAsync('dynamic/merge.xml?svData=&posData=', SkyPlot.processXmlData, SkyPlot.errorPage);
    }
};
SkyPlot.errorPage = function(doc) {
    SkyPlot.loading = false;
    for (var i = 0; i < SkyPlot.plots.length; ++i) showGetXmlError(doc, SkyPlot.plots[i].overallDiv, SkyPlot.plots[i].warningDiv);
    SkyPlot.getXmlData();
};
SkyPlot.processXmlData = function(doc) {
    SkyPlot.loading = false;
    SkyPlot.pageTimeout = setTimeout('SkyPlot.getXmlData()', SkyPlot.refreshInterval);
    var xmlData = xmlToObject(doc.documentElement);
    for (var i = 0; i < SkyPlot.plots.length; ++i) SkyPlot.plots[i].updateGraph(xmlData);
    if (SkyPlot.xmlCallback) SkyPlot.xmlCallback(xmlData.svTrack);
};
SkyPlot.prototype.updateGraph = function(xmlData) {
    showElemById(this.overallDiv);
    hideElemById(this.warningDiv);
    forcePropertyToArray(xmlData.svTrack, 'sv');
    var svData = xmlData.svTrack.sv;
    forcePropertyToArray(xmlData.pos.position.SvsUsed, 'sv');
    var posSvs = xmlData.pos.position.SvsUsed.sv;
    var surface = this.surface;
    var scale = this.scale;
    var svElem = this.svElem;
    var legendElem = this.legendElem;
    var dimLight = (trmbTop.brightness != 'day');
    svMaxSystem = 9;
    svTypeUnknown = svMaxSystem;
    var colors = ['#0000A0', '#007000', '#800000', '#806000', '#006060', '#800080', '#4444cc', '#804040', '#404080', 'gray'];
    var usedInPosColor = '#ff00ff';
    var SVTypes = new Array('GPS', 'SBAS', 'GLONASS', 'GALILEO', 'QZSS', 'BEIDOU', 'OMNI', 'XPS', 'IRNSS');
    var SVLabels = new Array('GPS', 'SBAS', 'GLN', 'GAL', 'QZSS', 'BDS', 'OMNI', 'XPS', 'IRNSS');
    switch (trmbTop.rxBrand) {
        case 1:
        case 3:
        case 4:
        case 5:
        case 8:
        case 9:
            SVLabels[6] = "FUGRO";
            colors = ['#98bd4a', '#D7DC1A', '#DEAE4E', '#6bb4db', '#006060', '#b469b9', '#0058b3', '#804040', '#404080', 'gray'];
            usedInPosColor = '#4c900e';
            break;
    }
    var i, j, s, e;
    var total = new Array(svMaxSystem);
    for (i = 0; i < svMaxSystem; ++i) total[i] = 0;
    for (e = 0, i = 0; i < svData.length; ++i) {
        forcePropertyToArray(svData[i], 'cno');
        if (svData[i].__antenna == 0) {
            var elev = parseFloat(svData[i].elev);
            if (elev == -45) continue;
            var azi = parseFloat(svData[i].azi) * Math.PI / 180;
            var cx = scale * (220 + Math.sin(azi) * (90 - elev) * (180 / 90));
            var cy = scale * (400 - (200 + Math.cos(azi) * (90 - elev) * (180 / 90)));
            var prn = svData[i].__PRN;
            var sys = svData[i].__sys;
            var type = svTypeUnknown;
            for (j = 0; j < svTypeUnknown; ++j) {
                if (sys.indexOf(SVTypes[j]) > -1) {
                    type = j; ++total[type];
                    break;
                }
            }
            var tracking = false;
            for (j = 0; j < svData[i].cno.length; ++j) if (svData[i].cno[j].__str > 0) {
                tracking = true;
                break;
            }
            var usedInPos = false;
            for (j = 0; j < posSvs.length; ++j) if (posSvs[j].__str - 0 == prn && posSvs[j].__sys == type) {
                usedInPos = true;
                break;
            }
            var fill = colors[type];
            var textFill = 'white';
            if (sys == 'OMNI' && !dimLight) textFill = 'black';
            var ringColor = 'black';
            var ringStroke = 1;
            if (usedInPos) {
                ringColor = usedInPosColor;
                ringStroke = 4;
            }
            if (!tracking) textFill = '#909090';
            var fontSize = scale * ((prn.toString().length <= 3) ? 15 : 10);
            if (!svElem[e]) {
                svElem[e] = new Object();
                svElem[e].shape = surface.createCircle({
                    r: scale * 12
                });
                svElem[e].text = surface.createText({
                    align: "middle"
                });
                svElem[e].ring = surface.createCircle({
                    r: scale * 12
                });
            }
            svElem[e].shape.setShape({
                cx: cx,
                cy: cy
            }).setFill(fill);
            svElem[e].ring.setShape({
                cx: cx,
                cy: cy
            }).setStroke({
                color: ringColor,
                width: ringStroke
            });
            svElem[e].text.setShape({
                x: cx,
                y: (cy + 6 * scale),
                text: prn
            }).setFill(textFill).setFont({
                family: "Arial",
                size: fontSize + "px"
            }); ++e;
        }
    }
    for (i = e; i < svElem.length; ++i) {
        svElem[i].shape.removeShape();
        svElem[i].text.removeShape();
        svElem[i].ring.removeShape();
    }
    svElem.length = e;
    for (e = 0, s = 0; s < svMaxSystem; ++s) {
        if (total[s] > 0) {
            if (!legendElem[e]) {
                legendElem[e] = new Object();
                legendElem[e].Rect = surface.createRect({
                    x: 420 * scale,
                    y: (e * 40 + 30) * scale,
                    width: 50 * scale,
                    height: 30 * scale
                });
                var size = 17;
                if (SVLabels[s].length > 4) size = 13;
                legendElem[e].Text = surface.createText({
                    x: 445 * scale,
                    y: (e * 40 + 50) * scale,
                    align: "middle"
                }).setFont({
                    family: "Arial",
                    size: size * scale + "px"
                });
            }
            legendElem[e].Rect.setFill(colors[s]);
            legendElem[e].Text.setShape({
                text: SVLabels[s]
            }).setFill('white'); ++e;
        }
    }
    for (i = e; i < svMaxSystem; ++i) if (legendElem[i]) {
        legendElem[i].Rect.removeShape();
        legendElem[i].Text.removeShape();
    }
    legendElem.length = e;
};